<?php

namespace addons\kzvote\controller;

use addons\kzvote\model\BannerModel;
use addons\kzvote\model\NavModel;
use addons\kzvote\model\TabbarModel;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use think\Config;
use think\Exception;

/**
 * 投票记录
 */
class Log extends Base
{
    /**
     * 导出
     */
    public function export()
    {
        set_time_limit(0);

        $vote_id = $this->request->param('vote_id');
        $vote_name = $this->request->param('vote_name');

        try {
            //开始制作表格
            $spreadsheet = new Spreadsheet();
            $spreadsheet->getDefaultStyle()->getFont()->setName('Microsoft Yahei');
            $spreadsheet->getDefaultStyle()->getFont()->setSize(12);
            $spreadsheet->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
            $spreadsheet->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
            $spreadsheet->getDefaultStyle()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            $spreadsheet->getActiveSheet()->getStyle('J')->getAlignment()->setWrapText(true);

            $worksheet = $spreadsheet->setActiveSheetIndex(0);


            $worksheet->getCell('A1')->setValue('投票活动');
            $worksheet->getCell('B1')->setValue('作品名称');
            $worksheet->getCell('C1')->setValue('投票用户');
            $worksheet->getCell('D1')->setValue('投票ip');
            $worksheet->getCell('E1')->setValue('投票方式');
            $worksheet->getCell('F1')->setValue('票数');
            $worksheet->getCell('G1')->setValue('投票时间');

            $ABC = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];

            $list = \app\common\model\kzvote\Log::with(['vote','person','user'])
                ->where('log.vote_id',$vote_id)
                ->order('createtime desc')
                ->select();

            $arr = [];
            foreach ($list as $key => $val) {

                $data = array($val['vote']['name'], $val['person']['name'], $val['user']['nickname'],$val['ip'],$val['type_text']
                ,$val['num'], date('Y-m-d H:i:s',$val['createtime']));

                array_push($arr, $data);
            }

            foreach ($arr as $key => $row) {
                foreach ($ABC as $k => $r) {
                    $worksheet->getCell($ABC[$k] . ($key + 2))->setValue($row[$k]);
                }
            }
            $dir = ROOT_PATH . 'public/export/';
            if(!is_dir($dir)){
                mkdir($dir, 0755);
            }
            $dir = ROOT_PATH . 'public/export/log/';
            if(!is_dir($dir)){
                mkdir($dir, 0755);
            }

            $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
            $writer->save(ROOT_PATH . 'public/export/log/《' . $vote_name.'》投票记录.xlsx');
            $this->success('导出成功',$this->request->domain().'/export/log/《' . $vote_name.'》投票记录.xlsx');
        }catch (Exception $e){
            $this->error($e->getMessage());
        }

    }


}
